home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 12.0 KB | 572 lines | [TEXT/MPS ] |
- /*
- File: MSAMSlot.cp
-
- Contains: xxx put contents here xxx
-
- Written by: Tim Harnett
-
- Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <18> 2/21/95 TMH metrowerks compatiblity changes
- <17> 2/16/95 TMH got rid of ProcessIncomingMail
- <16> 2/15/95 TMH removed assert on when slotID attribute is missing
- <15> 2/14/95 CL CAttribute & CRecord I/F changes
- <14> 2/14/95 TMH replace GetEWorldSlot and GetHFSSlot with GetExternalSlot
- <13> 2/10/95 TMH change wireless to HFS
- <13> 2/10/95 TMH change wireless to HFS
- <12> 12/20/94 TMH added IsLocationActive(), Busy()
- <11> 12/16/94 TMH added GetOutTrayLetterInfo(), IsLetterDone·
- <10> 12/12/94 TMH modified so slot Id is written only once
- <9> 10/18/94 TMH read the users name and password
- <8> 10/13/94 TMH added IsOurRecipient
- <7> 10/11/94 TMH CommThread integration
- <6> 10/6/94 TMH move LogError and GetLogEntry to LogErrors.cp
- <5> 10/3/94 TMH break of THFS&TEWorld from TExternalSlot
- <4> 9/22/94 TMH fix bug where queues not avail when creating slot, use
- GetOutQueueRef() rather than using fOutQRef directly
- <3> 9/22/94 TMH added use of CQueueEnumerator.cp,.h
- <2> 9/21/94 TMH Use new Debug.cp ASSERT stuff
- <1> 9/20/94 TMH Abandon RoadsideRest embrace Mercury
- <3> 9/20/94 TMH bug fix in TMSAMSlot::GetOutgoingLetter(), unintended call to
- toolbox async
- <2> 9/9/94 TMH TIncoming/TOutgoing adaptations
- 4/12/94 TMH xxx put comment here xxx
-
- To Do:
- */
-
- #ifndef __RESOURCES__
- #include "Resources.h"
- #endif
-
- #ifndef __OCEERRORS__
- #include "OCEErrors.h"
- #endif
-
- #ifndef __Globals__
- #include "Globals.h"
- #endif
-
- #ifndef __Application__
- #include "Application.h"
- #endif
-
- #ifndef __Debug__
- #include "Debug.h"
- #endif
-
- #ifndef __SetupInfoDefinitions__
- //#include "SetupInfoDefinitions.h"
- #endif
-
- #ifndef __MSAMSlot__
- #include "MSAMSlot.h"
- #endif
-
- #ifndef __EWorldSlot__
- //#include "EWorldSlot.h"
- #endif
-
- #ifndef __ExternalSlot__
- #include "ExternalSlot.h"
- #endif
-
- #ifndef __CAttribute__
- #include "CAttribute.h"
- #endif
-
- #ifndef __CDSSpec__
- #include "CDSSpec.h"
- #endif
-
- #ifndef __CQueueEnumerator__
- #include "CQueueEnumerator.h"
- #endif
-
- #ifndef __FileUtils__
- #include "FileUtils.h"
- #endif
-
-
- #ifndef __ALetter__
- #include "ALetter.h"
- #endif
-
- #ifndef __XLetter__
- #include "XLetter.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
-
- #ifndef __LogErrors__
- #include "LogErrors.h"
- #endif
-
- #ifndef __SlotThread__
- #include "SlotThread.h"
- #endif
-
-
- //--------------------------------------
- // T M S A M S l o t
- //--------------------------------------
-
-
- #pragma segment MSAMSlot
-
- //-------------------------------------------------------------------------------------
- TMSAMSlot::TMSAMSlot()
- {
- fSlotRecord = 0;
- fSlotID = 0;
- fDirectoryRecord = 0;
- // fDirDiscriminator = gZeroDirDiscriminator;
- fDirDiscriminator.signature = fDirDiscriminator.misc = 0;
-
-
- fInQueueRef = 0;
- fOutQueueRef = 0;;
-
- memset(&fStdSlotInfo,0,sizeof(MailStandardSlotInfoAttribute));
-
- fSlotThread = 0;
-
- fExternalSlot = 0;
-
-
- }
-
-
- //-------------------------------------------------------------------------------------
- TMSAMSlot::~TMSAMSlot()
- {
- if( fDirectoryRecord != 0 ) {
- delete fDirectoryRecord;
- fDirectoryRecord = 0;
- }
-
- if( fSlotRecord != 0 ) {
- delete fSlotRecord;
- fSlotRecord = 0;
- }
-
- if( fExternalSlot != 0 ) {
- delete fExternalSlot;
- fExternalSlot = 0;
- }
-
- if( fSlotThread != 0 ) {
- delete fSlotThread;
- fSlotThread = 0;
- }
-
-
- }
-
-
- //-------------------------------------------------------------------------------------
- short TMSAMSlot::GetSlotID()
- {
- if( fSlotID == 0 && fSlotRecord != 0 ) {
-
- CAttribute slotIDAttr(kSlotIDAttrTypeNum,'bnry');
- long slotIDsize = sizeof(short);
- short slotID = 0;
- OSErr osErr = slotIDAttr.Read1Value(*fSlotRecord,&slotID,&slotIDsize, gIdentity);
- if( osErr == 0 && slotIDsize == 2 )
- fSlotID = slotID;
-
- //•• failures
- }
-
- return fSlotID;
- }
-
-
-
- //-------------------------------------------------------------------------------------
- OSErr TMSAMSlot::Activate()
- {
-
- return this->OpenQueues();
- }
-
-
- //-------------------------------------------------------------------------------------
- OSErr TMSAMSlot::OpenQueues()
- {
- short slotID = this->GetSlotID();
- OSErr osErr = 0;
-
- if( slotID != 0 ) {
-
- MSAMParam pb;
- memset(&pb,0,sizeof(MSAMParam));
- pb.pmsamOpenQueues.msamSlotID = fSlotID;
-
- osErr = PMSAMOpenQueues(&pb);
- if( osErr == 0 ) {
- fInQueueRef = pb.pmsamOpenQueues.inQueueRef;
- fOutQueueRef = pb.pmsamOpenQueues.outQueueRef;
- }
-
- }
-
- return osErr;
- }
-
-
-
- //-------------------------------------------------------------------------------------
- MSAMQueueRef TMSAMSlot::GetOutQueueRef()
- {
- if( fOutQueueRef == 0 )
- this->OpenQueues();
-
- ASSERT(fOutQueueRef!=0);
-
- return fOutQueueRef;
- }
-
- //-------------------------------------------------------------------------------------
- MSAMQueueRef TMSAMSlot::GetInQueueRef()
- {
- if( fInQueueRef == 0 )
- this->OpenQueues();
-
- ASSERT(fInQueueRef!=0);
-
- return fInQueueRef;
-
- }
-
-
-
- //-------------------------------------------------------------------------------------
- void TMSAMSlot::IMSAMSlot(CreationID* slotCID)
- {
-
- // Assumes the slot has been "Created" ie. all of its neccessary
- // attributes are available in the keychain.
-
-
- OSErr osErr = 0;
-
-
- FailInfo fi;
- Try(fi) {
-
-
- fSlotRecord = new CRecordID(gKeyChainDSRefnum,*slotCID);
- osErr = fSlotRecord->GetNameAndType();
- ASSERTNOERR(osErr);
- FailOSErr(osErr);
-
-
- // Slot id
-
-
- CAttribute theAttr(kSlotIDAttrTypeNum);
- osErr = theAttr.Read1Value(*fSlotRecord,&fSlotID,sizeof(short), gIdentity);
- if( osErr == kOCENoSuchAttributeValue ) {
- fSlotID = -1; // we are in the process of creating the slot.
- osErr = 0;
- }
- ASSERTNOERR(osErr);
- FailOSErr(osErr);
-
-
- // Get Directory Info
-
-
- osErr = this->ReadDirectoryInfo();
- ASSERTNOERR(osErr);
- FailOSErr(osErr);
-
-
- // The HFS Slot
-
-
- TExternalSlot* extSlot = new TExternalSlot;
- extSlot->IExternalSlot(this);
- fExternalSlot = extSlot;
-
-
- // A Thread For us.
-
-
- TSlotThread* slotThread = new TSlotThread();
- slotThread->ISlotThread(this);
- fSlotThread = slotThread;
-
-
- fi.Success();
-
- } else {
-
- delete this;
- fi.ReSignal();
-
- }
-
- }
-
-
- //-------------------------------------------------------------------------------------
- OSErr TMSAMSlot::ReadDirectoryInfo()
- {
-
-
- // Our Directory Record
-
-
- #define kKeyChainMaxPackedRIDLen 200
- char packedRID[kKeyChainMaxPackedRIDLen];
-
-
- CAttribute assocDir(kAssoDirectoryAttrTypeNum);
- OSErr osErr = assocDir.Read1Value(*fSlotRecord,&packedRID,kKeyChainMaxPackedRIDLen, gIdentity);
- ASSERTNOERR(osErr);
- if( osErr != 0 )
- return errMSAMSetupInfoCorrupt;
-
-
- fDirectoryRecord = new CRecordID(gKeyChainDSRefnum,(PackedRecordID*)packedRID);
-
-
- osErr = fDirectoryRecord->GetNameAndType();
- ASSERTNOERR(osErr);
- if( osErr != 0 )
- return errMSAMSetupInfoCorrupt;
-
-
- // Our Discriminator
-
-
- CAttribute discAttr(kDiscriminatorAttrTypeNum);
- osErr = discAttr.Read1Value(*fDirectoryRecord,&fDirDiscriminator,sizeof(DirDiscriminator), gIdentity);
- ASSERTNOERR(osErr);
-
- if( osErr == 0 )
- osErr = this->ReadExSysUserNameAndPassword();
-
- if( osErr != 0 )
- osErr = errMSAMSetupInfoCorrupt;
-
- return osErr;
- }
-
-
- //-------------------------------------------------------------------------------------
- char* TMSAMSlot::GetExSysPassword(char* password)
- {
-
- // Refresh the cached values. They may have changed.
-
- this->ReadExSysUserNameAndPassword();
-
- fPassword.CopyBody(password);
- password[fPassword.BodyLength()] = 0;
- return password;
- }
-
-
-
- //-------------------------------------------------------------------------------------
- char* TMSAMSlot::GetExSysUserName(char* userName)
- {
- // Refresh the cached values. They may have changed.
-
- this->ReadExSysUserNameAndPassword();
-
- fUserName.CopyBody(userName);
- userName[fUserName.BodyLength()] = 0;
- return userName;
-
- }
-
-
-
- //-------------------------------------------------------------------------------------
- OSErr TMSAMSlot::ReadExSysUserNameAndPassword()
- {
-
-
- OCESetupGetDirectoryInfoPB dirInfoPB;
- memset(&dirInfoPB,0,sizeof(OCESetupGetDirectoryInfoPB));
-
- CRecordID userRec;
- CRecordName nativeName; // don't care about this
-
- dirInfoPB.dsRefNum = gKeyChainDSRefnum;
- dirInfoPB.identity = gIdentity;
-
- CDirectoryName dirName(this->GetDirectoryName());
- dirInfoPB.directoryName = dirName;
- dirInfoPB.discriminator = fDirDiscriminator;
-
- dirInfoPB.recordID = userRec;
- dirInfoPB.nativeName = (RStringPtr)&nativeName;
- dirInfoPB.password = (RStringPtr)&fPassword; // we cache this
-
-
- OSErr osErr = OCESetupGetDirectoryInfo((AuthParamBlockPtr)&dirInfoPB,false);
-
- if( osErr == 0 )
- fUserName = userRec.Name(); // ... and this
-
- osErr = 0; //••• testing
- return osErr;
- }
-
-
-
- //-------------------------------------------------------------------------------------
- Boolean TMSAMSlot::IsOurRecipient(CUnpackedDSSpec& recipient)
- {
-
- if( recipient.fExtType != fDirDiscriminator.signature )
- return false;
-
-
- return recipient.DirName() == (CDirectoryName&) fDirectoryRecord->Name();
-
-
-
- }
-
-
- //-------------------------------------------------------------------------------------
- OSErr TMSAMSlot::SetSlotInfo()
- {
-
- // Get standard slot information
-
- CAttribute theAttr(kStdSlotInfoAttrTypeNum);
- OSErr osErr = theAttr.Read1Value(*fSlotRecord,&fStdSlotInfo,sizeof(MailStandardSlotInfoAttribute), gIdentity);
- ASSERTNOERR(osErr);
- FailOSErr(osErr);
-
-
- // ...TBD: there is more config info to get here.
-
-
- // Configure the external slot --
- if( fExternalSlot != 0 )
- fExternalSlot->SetConfiguration();
-
-
- return osErr;
- }
-
-
-
- #pragma segment MSAMSlot
- //-----------------------------------------------------------------------------------
- TOutgoingALetter* TMSAMSlot::GetOutgoingLetter()
- {
-
- TOutgoingALetter* outgoingLetter = 0;
- COutQEnumerator iter( this->GetOutQueueRef() );
-
- for(MSAMEnumerateOutQReply* enumReply = iter.FirstLetter(); iter.More(); enumReply=iter.NextLetter()) {
-
- if( !enumReply->done ) {
- outgoingLetter = new TOutgoingALetter;
- outgoingLetter->IOutgoingALetter(this,enumReply);
- break;
- }
-
- }
-
- return outgoingLetter;
- }
-
- //-----------------------------------------------------------------------------------
- long TMSAMSlot::CountOutgoingLetters()
- {
-
- long noutgoingLetters = 0;
- COutQEnumerator iter( this->GetOutQueueRef() );
- for(MSAMEnumerateOutQReply* enumReply = iter.FirstLetter(); iter.More(); enumReply=iter.NextLetter())
- if( !enumReply->done )
- noutgoingLetters++;
-
- return noutgoingLetters;
-
- }
-
-
- //-----------------------------------------------------------------------------------
- OSErr TMSAMSlot::GetOutTrayLetterInfo(long letterSeqNo,MSAMEnumerateOutQReply& outQReply)
- {
- MSAMEnumeratePB pb;
- EnumOutQReply enumBlock;
-
- pb.ioCompletion = 0;
- pb.queueRef = this->GetOutQueueRef();
- pb.startSeqNum = letterSeqNo;
- pb.buffer.bufferSize = sizeof(EnumOutQReply);
- pb.buffer.buffer = (Ptr)&enumBlock;
-
- MSAMEnumerate((MSAMParam *)&pb,true);
- do {
- gCurrentThread->Yield();
- } while( pb.ioResult > 0 );
-
-
- ASSERTNOERR(pb.ioResult);
- if( pb.ioResult == 0 )
- BlockMoveData(&enumBlock.outQEnumReply,&outQReply,sizeof(MSAMEnumerateOutQReply));
-
- return pb.ioResult;
-
- }
-
-
- //-----------------------------------------------------------------------------------
- Boolean TMSAMSlot::IsLetterDone(long letterSeqNo)
- {
-
- Boolean isDone = true;
-
- MSAMEnumerateOutQReply outTrayLetterInfo;
- OSErr osErr = this->GetOutTrayLetterInfo(letterSeqNo,outTrayLetterInfo);
- if( osErr == 0 )
- isDone = outTrayLetterInfo.done;
-
- return isDone;
-
- }
-
-
- //-----------------------------------------------------------------------------------
- Boolean TMSAMSlot::Busy()
- {
- return (fSlotThread != 0) && (fSlotThread->GetThreadState() != kStoppedThreadState);
- }
-
-
- //-----------------------------------------------------------------------------------
- Boolean TMSAMSlot::IsLocationEnabled()
- {
- Boolean enabled = false;
-
- MailLocationFlags ourLocation = gApplication->Location();
-
- return ( fStdSlotInfo.active & MailLocationMask(ourLocation) ) != 0;
-
- }
-
-
- //-----------------------------------------------------------------------------------
- TExternalSlot* TMSAMSlot::GetExternalSlot()
- {
- return (TExternalSlot*) fExternalSlot;
- }
-